﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Collections;

namespace DriveBabyDrive.Util
{
    public class CommonFunctions
    {
       
        public static string GetJson(DataTable dt){
            /*JavaScriptSerializer serializer=  new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col.ColumnName]);

                }
                rows.Add(row);
            }

            return serializer.Serialize(rows);*/
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string dtJson = new Bortosky.Google.Visualization.GoogleDataTable(dt).GetJson();
            return dtJson;
        }
        /// <summary>
        /// Builds and returns a select list used to bind a list box or a drop down list on the view
        /// </summary>
        /// <param name="dt">Table to get data from</param>
        /// <param name="idCol">Id column</param>
        /// <param name="valCol">Value column</param>
        /// <returns></returns>
        public static SelectList getSelectListFromDataTable(DataTable dt, string idCol, string valCol, IEnumerable selectedValues)
        {
            ArrayList arr = new ArrayList();
            foreach (DataRow vehicle in dt.Rows)
            {


                arr.Add(new { id = vehicle[idCol].ToString(), value = vehicle[valCol].ToString() });
            }
            SelectList retList;
            if (selectedValues == null)
            {
                retList = new SelectList(arr.ToArray(), "id", "value");
            }
            else
            {
                retList = new SelectList(arr.ToArray(), "id", "value", selectedValues);
            }

            return retList;
        }
        public static MultiSelectList getMultiSelectListFromDataTable(DataTable dt, string idCol, string valCol,IEnumerable selectedValues)
        {
            ArrayList arr = new ArrayList();
            foreach (DataRow vehicle in dt.Rows)
            {
                
                
                arr.Add(new  { id = vehicle[idCol].ToString(), value = vehicle[valCol].ToString() });
            }
            MultiSelectList retList;
            if (selectedValues == null)
            {
                 retList=new MultiSelectList(arr.ToArray(), "id", "value");
            }
            else
            {
                retList=new MultiSelectList(arr.ToArray(), "id", "value",selectedValues);
            }

            return retList;
        }

    }
}